package org.springblade.modules.admin.controller.parts.shop;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.config.ProjectApiConfiguration;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.utils.SkuInfoUtil;
import org.springblade.modules.admin.vo.PartsShopUnitVO;
import org.springblade.modules.admin.vo.parts.shop.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author ysq
 * @Date 2024/09/03
 * @Description 接入配件商城接口
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("admin/parts/shop")
@Api(value = "配件商城接口", tags = "配件商城接口")
public class PartsShopController {

	private final ProjectApiConfiguration projectApiConfiguration;

	/**
	 * 配件类目-懒加载列表
	 */
	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "parentId", value = "父节点id", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "code", value = "类目编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "类目名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "parentCode", value = "父类目编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "parentName", value = "父类目名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "ids", value = "id集合", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "配件类目-懒加载列表")
	public R<List<CategoryVO>> lazyList(@ApiIgnore @RequestParam Map<String, Object> param) {
		String url = projectApiConfiguration.getParts() + "/parts/category/lazy-list";
		String res = null;
		try {
			res = HttpUtil.get(url, param, 10000);
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			return R.fail("请求配件商城接口失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(res);
		if (jsonObject.getBoolean("success")) {
			List<CategoryVO> list = jsonObject.getJSONArray("data").toJavaList(CategoryVO.class);
			return R.data(ForestNodeMerger.merge(list));
		} else {
			return R.fail(jsonObject.getString("msg"));
		}
	}

	/**
	 * sku列表
	 */
	@GetMapping("/sku-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "sku列表")
	public R<List<SkuInfoSelectedVO>> skuList(@ApiIgnore @RequestParam Map<String, Object> param) {
		String url = projectApiConfiguration.getParts() + "/parts/skuinfo/getSkuInfoList";
		String res = null;
		try {
			res = HttpUtil.get(url, param, 10000);
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			return R.fail("请求配件商城接口失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(res);
		if (jsonObject.getBoolean("success")) {
			List<SkuInfoListVO> list = jsonObject.getJSONArray("data").toJavaList(SkuInfoListVO.class);
			List<SkuInfoSelectedVO> attrList = new ArrayList<>(list.size());
			for (SkuInfoListVO skuInfo : list) {
				String attr = SkuInfoUtil.attrHandle(skuInfo.getBrandName(), skuInfo.getSpuName(),skuInfo.getAttr());
				SkuInfoSelectedVO selected = new SkuInfoSelectedVO();
				selected.setSkuId(skuInfo.getSkuId());
				selected.setAttr(attr);
				attrList.add(selected);
			}
			return R.data(attrList);
		} else {
			return R.fail(jsonObject.getString("msg"));
		}
	}

	@GetMapping("/sku-detail")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "sku详情")
	public R<SkuDetailVO> skuDetail(@ApiParam(value = "skuId", required = true) @RequestParam Long skuId) {
		String url = projectApiConfiguration.getParts() + "/parts/skuinfo/detail?enable=1&id=" + skuId;
		String res = null;
		try {
			res = HttpUtil.get(url, 10000);
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			return R.fail("请求配件商城接口失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(res);
		if (jsonObject.getBoolean("success")) {
			SkuDetailVO detail = jsonObject.getJSONObject("data").toJavaObject(SkuDetailVO.class);
			return R.data(detail);
		} else {
			return R.fail(jsonObject.getString("msg"));
		}
	}

	@GetMapping("/get-attr-list")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "根据类目查询属性信息")
	public R<List<AttrInfoVO>> getAttrListByCategoryId(@ApiParam(value = "类目id", required = true) @RequestParam Long categoryId,
									 @ApiParam(value = "属性分类[0-关键属性, 1-销售属性, 2-描述属性],多个逗号隔开") @RequestParam(required = false) String types) {
		Map<String, Object> param = MapUtil.ofEntries(MapUtil.entry("categoryId", categoryId), MapUtil.entry("types", types));
		String url = projectApiConfiguration.getParts() + "/parts/attr/getAttrListByCategoryId";
		String res = null;
		try {
			res = HttpUtil.get(url, param, 10000);
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			return R.fail("请求配件商城接口失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(res);
		if (jsonObject.getBoolean("success")) {
			List<AttrInfoVO> list = jsonObject.getJSONArray("data").toJavaList(AttrInfoVO.class);
			return R.data(list);
		} else {
			return R.fail(jsonObject.getString("msg"));
		}
	}

	/**
	 * sku列表
	 */
	@GetMapping("/sku-detail_list")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "sku详情列表")
	public R<List<SkuInfoListVO>> skuDetailList(@ApiIgnore @RequestParam Map<String, Object> param) {
		String url = projectApiConfiguration.getParts() + "/parts/skuinfo/getSkuInfoList";
		String res = null;
		try {
			res = HttpUtil.get(url, param, 10000);
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			return R.fail("请求配件商城接口失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(res);
		if (jsonObject.getBoolean("success")) {
			List<SkuInfoListVO> list = jsonObject.getJSONArray("data").toJavaList(SkuInfoListVO.class);
			return R.data(list);
		} else {
			return R.fail(jsonObject.getString("msg"));
		}
	}

	/**
	 * sku列表
	 */
	@GetMapping("/sku-detail_page")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "sku详情分页")
	public R<IPage<SkuInfoListVO>> skuDetailPage(@ApiIgnore @RequestParam Map<String, Object> param) {
		String url = projectApiConfiguration.getParts() + "/parts/skuinfo/getSkuInfoPage";
		String res = null;
		try {
			res = HttpUtil.get(url, param, 10000);
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			return R.fail("请求配件商城接口失败");
		}

		JSONObject jsonObject = JSONObject.parseObject(res);
		if (jsonObject.getBoolean("success")) {
			IPage<SkuInfoListVO> page = jsonObject.getJSONObject("data").toJavaObject(IPage.class);
			return R.data(page);
		} else {
			return R.fail(jsonObject.getString("msg"));
		}
	}

	/**
	 * 获取单位接口
	 */
	@GetMapping("/get-unit-list")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取单位列表")
	public R<List<PartsShopUnitVO>> getUnitList() {
		String url = projectApiConfiguration.getParts() + "/parts/unit/list";
		try {
			String res = HttpUtil.get(url, 10000);
			JSONObject jsonObject = JSONObject.parseObject(res);
			if(Func.isNotEmpty(jsonObject)  && jsonObject.containsKey("data")){
				JSONArray jsonArray = jsonObject.getJSONArray("data");
				// 转换成VO
				return R.data(jsonArray.toJavaList(PartsShopUnitVO.class));
			}
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			throw new ServiceException("请求配件商城接口失败");
		}
		return R.data(new ArrayList<>());
	}

	/**
	 * 获取单位类型接口
	 */
	@GetMapping("/get-unit-value")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "获取单位类型列表")
	public R<List<String>> getUnitValueList() {
		String url = projectApiConfiguration.getParts() + "/parts/unit/unitValueList";
		try {
			String res = HttpUtil.get(url, 10000);
			JSONObject jsonObject = JSONObject.parseObject(res);
			if(Func.isNotEmpty(jsonObject)  && jsonObject.containsKey("data")){
				JSONArray jsonArray = jsonObject.getJSONArray("data");
				// 转换成VO
				return R.data(jsonArray.toJavaList(String.class));
			}
		} catch (HttpException e) {
			log.error("请求配件商城接口失败，url: " + url);
			throw new ServiceException("请求配件商城接口失败");
		}
		return R.data(new ArrayList<>());
	}

}
